--require('collectible')
--require('view')


--[[

	World Functionality
	
--]]
--Initialize maximum size values
function init()
	maxX = 10000 --To be changed
	maxY = 10000 --To be changed
	totalCollectibles = 50
	
	--These lines represent distance from the center in pixels
	zoneEdges = {500, 1000, 1500}
	
	--This represents current number of items in each zone
	itemsInZones = {0, 0, 0}
	
	--These represent percentage of total collectibles in each zone
	zonePercentages = {.15, .3, .65}
	
	--This represents border around the edge of the map that no collectibles will spawn in
	borderSize = 50
	
	spawnCollectibles()
	drawMap()
end

--Initializes the map
function initMap()
	-- our tiles (this will be removed once we decide on the background
    tile = {}
    for i=0,3 do
        tile[i] = love.graphics.newImage( "tile"..i..".png" )
    end
    
    -- the map (also to be removed)
    map={
    { 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0}, 
    { 3, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0},
    { 3, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0},
    { 3, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0},
    { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2},
    { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
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 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    }
        
    -- Total dimensions of map
    map_w = 21
    map_h = 29
    
    map_display_buffer = 1
    
    -- How many tiles to display to the screen;
    -- will correspond to the screen size
    map_display_w = 15
    map_display_h = 10
    
    tile_w = 64
    tile_h = 64
end	

--Spawns collectible items
function spawnCollectibles()
	math.randomseed(os.time())
	
	currentCollectibles = 0
	while currentCollectibles < totalCollectibles do
		correctSection = 0
		while correctSection == 0 do
			--Generate random coordinates
			randXCoor = math.floor((math.random() * 100000)) % maxX
			randYCoor = math.floor((math.random() * 100000)) % maxY
			
			--Calculate zone of generated coordinates
			center = {(maxX/2), (maxY/2)}
			distance = math.sqrt(math.pow((randXCoor - center[0]), 2) + math.pow((randYCoor - center[1]), 2)) 
			if distance < 500 then
				currentZone = 0
			end
			if (distance > 499) and (distance < 1000) then
				currentZone = 1
			end
			if (distance > 999) and (distance < maxX) then
				currentZone = 2
			end
			
			--Calculate necessary zone
			if currentCollectibles ~= 0 then
				currentZonePercentages[0] = inZones[0] / currentCollectibles
				currentZonePercentages[1] = inZones[1] / currentCollectibles
				currentZonePercentages[2] = inZones[2] / currentCollectibles
			else
				currentZonePercentages = {0, 0, 0}
			end
			
			zoneDissonance = {0, 0, 0}
			zoneDissonance[0] = zonePercentages[0] - currentZonePercentages[0]
			zoneDissonance[1] = zonePercentages[1] - currentZonePercentages[1]
			zoneDissonance[2] = zonePercentages[2] - currentZonePercentages[2]
			
			desiredZone = 0
			if zoneDissonance[1] > zoneDissonance[0] then
				desiredZone = 1
			end
			if zoneDissonance[2] > zoneDissonance[desiredZone] then
				desiredZone = 2
			end
			
			--Check to see if generated collectible is in desired zone
			if currentZone == desiredZone then
				correctSection = 1
			end
			
			--Check to see if collectible is in borders
			if (randXCoor < borderSize) or (randXCoor > (maxX - borderSize)) then
				correctSection = 0
			end
			if (randYCoor < borderSize) or (randYCoor > (maxY - borderSize)) then
				correctSection = 0
			end
			
			if correctSection == 1 then
				collectiblePositions[currentCollectibles] = {randXCoor, randYCoor}
				--collectible:spawn({randXCoor, randYCoor}, "energy")
				currentCollectibles = currentCollectibles + 1
			end
		end
	end
end

--Draws the map    
function drawMap()
    offset_x = (view:getViewCenter())[0] % tile_w
    offset_y = (view:getViewCenter())[1] % tile_h
    
    -- Top left tile on the player's screen
    firstTile_x = math.floor((view:getViewCenter())[0] / tile_w)
    firstTile_y = math.floor((view:getViewCenter())[1] / tile_h)
    
    for y=1, (map_display_h + map_display_buffer) do
        for x=1, (map_display_w + map_display_buffer) do
            -- Note that this condition block allows us to go beyond the edge of the map.
            if y+firstTile_y >= 1 and y+firstTile_y <= map_h
                and x+firstTile_x >= 1 and x+firstTile_x <= map_w
            then
                love.graphics.draw(
                    tile[map[y+firstTile_y][x+firstTile_x]], 
                    (x*tile_w) - offset_x - tile_w, 
                    (y*tile_h) - offset_y - tile_h)
            end
        end
    end
end

--[[

	Gets and Sets
	
--]]
--Returns maximum x value for map
function getMaxX()
	return maxX
end

function getMaxY()
	return maxY
end